package com.example.wei.mvp.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.wei.mvp.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 温馨提示
 * Created by win7 on 2017/7/21.
 */

public class MyAlertDialog extends Dialog {


    private String title;
    @BindView(R.id.tv_ts_text)
    TextView tvTsText;
    @BindView(R.id.tv_ts_quit)
    TextView tvTsQuit;
    @BindView(R.id.tv_ts_again)
    TextView tvTsAgain;
    private Activity activity;
    private String content;
    private String leftButton;
    private String rightButton;
    private View contentView;
    private OnDialogButtonClickListener buttonClickListner;

    public MyAlertDialog(Activity activity, String title, String content, String leftButton, String rightButton) {
        super(activity, R.style.MyDialogStyleTop);
        this.content = content;
        this.activity = activity;
        this.leftButton = leftButton;
        this.rightButton = rightButton;
        this.title = title;
        iniView();
    }

    private void iniView() {
        contentView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_tishi_message, null);
        ButterKnife.bind(this, contentView);
        tvTsQuit.setText(leftButton);
        tvTsAgain.setText(rightButton);
        tvTsText.setText(content);
        show();
        setContentView(contentView);
        WindowManager m = activity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
        getWindow().setAttributes(p);
        setCanceledOnTouchOutside(false);
        setCancelable(false);

    }

    @OnClick({R.id.tv_ts_quit, R.id.tv_ts_again})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_ts_quit:
                if (buttonClickListner != null) {
                    buttonClickListner.onButtonLeftClick();
                }
                dismiss();
                break;
            case R.id.tv_ts_again:
                if (buttonClickListner != null) {
                    buttonClickListner.onButtonRightClick();
                }
                dismiss();
                break;
        }
    }

    //实现回调功能
    public interface OnDialogButtonClickListener {
        void onButtonLeftClick();

        void onButtonRightClick();

    }

    public void setOnButtonClickListener(OnDialogButtonClickListener listener) {
        this.buttonClickListner = listener;
    }


}
